Socket
Socket
Sign inDemoInstall

discord.js

Package Overview
Dependencies
25
Maintainers
3
Versions
1650
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    discord.js

A powerful library for interacting with the Discord API


Version published
Maintainers
3
Created

Package description

What is discord.js?

discord.js is a powerful Node.js module that allows you to interact with the Discord API very easily. It provides a comprehensive set of features to create bots and manage Discord servers.

What are discord.js's main functionalities?

Creating a Bot

This code demonstrates how to create a simple Discord bot using discord.js. The bot logs 'Ready!' to the console when it is successfully logged in and ready.

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once('ready', () => {
  console.log('Ready!');
});
client.login('your-token-goes-here');

Handling Messages

This code shows how to handle messages in a Discord server. When a user sends a message with the content '!ping', the bot responds with 'Pong!'.

client.on('messageCreate', message => {
  if (message.content === '!ping') {
    message.channel.send('Pong!');
  }
});

Managing Roles

This code demonstrates how to manage roles in a Discord server. When a user sends a message with the content '!addRole', the bot adds a role named 'NewRole' to the user.

client.on('messageCreate', async message => {
  if (message.content === '!addRole') {
    let role = message.guild.roles.cache.find(r => r.name === 'NewRole');
    if (role) {
      await message.member.roles.add(role);
      message.channel.send('Role added!');
    }
  }
});

Sending Embeds

This code shows how to send embedded messages in Discord. When a user sends a message with the content '!embed', the bot responds with a rich embed message.

const { MessageEmbed } = require('discord.js');
client.on('messageCreate', message => {
  if (message.content === '!embed') {
    const embed = new MessageEmbed()
      .setTitle('Sample Embed')
      .setDescription('This is an example of an embed message')
      .setColor(0xff0000);
    message.channel.send({ embeds: [embed] });
  }
});

Other packages similar to discord.js

Readme

Source

discord.js


Discord server npm version npm downloads Tests status Code coverage

Vercel Cloudflare Workers

About

discord.js is a powerful Node.js module that allows you to easily interact with the Discord API.

  • Object-oriented
  • Predictable abstractions
  • Performant
  • 100% coverage of the Discord API

Installation

Node.js 16.11.0 or newer is required.

npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js

Optional packages

  • zlib-sync for WebSocket data compression and inflation (npm install zlib-sync)
  • bufferutil for a much faster WebSocket connection (npm install bufferutil)
  • utf-8-validate in combination with bufferutil for much faster WebSocket processing (npm install utf-8-validate)
  • @discordjs/voice for interacting with the Discord Voice API (npm install @discordjs/voice)

Example usage

Install discord.js:

npm install discord.js
yarn add discord.js
pnpm add discord.js
bun add discord.js

Register a slash command against the Discord API:

import { REST, Routes } from 'discord.js';

const commands = [
  {
    name: 'ping',
    description: 'Replies with Pong!',
  },
];

const rest = new REST({ version: '10' }).setToken(TOKEN);

try {
  console.log('Started refreshing application (/) commands.');

  await rest.put(Routes.applicationCommands(CLIENT_ID), { body: commands });

  console.log('Successfully reloaded application (/) commands.');
} catch (error) {
  console.error(error);
}

Afterwards we can create a quite simple example bot:

import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('interactionCreate', async interaction => {
  if (!interaction.isChatInputCommand()) return;

  if (interaction.commandName === 'ping') {
    await interaction.reply('Pong!');
  }
});

client.login(TOKEN);

Extensions

Contributing

Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the documentation.
See the contribution guide if you'd like to submit a PR.

Help

If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official discord.js Server.

Keywords

FAQs

Last updated on 02 Jun 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc